<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class FishingField extends WdController {

	/**
	 * 钓场编辑
	 */
	public function edit(){

		$apiid = isset($_GET["apiid"]) ? intval($_GET["apiid"]) : 0;

		$api = \libraries\waydiao\Wdata::getApi($apiid);

		$data = [
			'hosts' => $this->config->item('api_host_yuxun'),
			'api' => $api,
		];

		$this->load->view('fishingField/edit', $data);

	}

	public function asyncEdit(){

		$api_id = isset($_POST["api_id"]) ? intval($_POST["api_id"]) : 0;
		$api_host = isset($_POST["api_host"]) ? addslashes($_POST["api_host"]) : "";
		$Authorization = $this->access_token;

		$fid = isset($_POST["fid"]) ? addslashes($_POST["fid"]) : "";
		$name = isset($_POST["name"]) ? addslashes($_POST["name"]) : "";
		$address = isset($_POST["address"]) ? addslashes($_POST["address"]) : "";
		$lat = isset($_POST["lat"]) ? addslashes($_POST["lat"]) : "";
		$lng = isset($_POST["lng"]) ? addslashes($_POST["lng"]) : "";
		$province = isset($_POST["province"]) ? addslashes($_POST["province"]) : "";
		$city = isset($_POST["city"]) ? addslashes($_POST["city"]) : "";
		$district = isset($_POST["district"]) ? addslashes($_POST["district"]) : "";
		$area = isset($_POST["area"]) ? addslashes($_POST["area"]) : "";
		$fish_species = isset($_POST["fish_species"]) ? addslashes($_POST["fish_species"]) : "";
		$owner = isset($_POST["owner"]) ? addslashes($_POST["owner"]) : "";
		$mobile = isset($_POST["mobile"]) ? addslashes($_POST["mobile"]) : "";

		echo $api_id."<br>";
		echo $api_host."<br>";
		echo $Authorization."<br>";

		$api = \libraries\waydiao\Wdata::getApi($api_id);

		$request_url = $api_host.$api["url"];
		echo $request_url."<br>";

		$data = [
			'id' => $fid,
			'name' => $name,
			'address' => $address,
			'lat' => $lat,
			'lng' => $lng,
			'province' => $province,
			'city' => $city,
			'district' => $district,
			'area' => $area,
			'fish_species' => $fish_species,
			'owner' => $owner,
			'mobile' => $mobile,
		];

		pre($data);

		$header = ["Authorization:Bearer ".$Authorization];

		$r = curl_post_header($request_url, json_encode($data), $header);

		var_dump($r);

		$r2 = json_decode($r);
		pre($r2);

	}

	/**
	 * 钓场删除
	 */
	public function remove(){

		$apiid = isset($_GET["apiid"]) ? intval($_GET["apiid"]) : 0;

		$api = \libraries\waydiao\Wdata::getApi($apiid);

		$data = [
			'hosts' => $this->config->item('api_host_yuxun'),
			'api' => $api,
		];

		$this->load->view('fishingField/remove', $data);

	}

	public function asyncRemove(){

		$api_id = isset($_POST["api_id"]) ? intval($_POST["api_id"]) : 0;
		$api_host = isset($_POST["api_host"]) ? addslashes($_POST["api_host"]) : "";
		$Authorization = $this->access_token;

		$fid = isset($_POST["fid"]) ? addslashes($_POST["fid"]) : "";

		echo $api_id."<br>";

		$api = \libraries\waydiao\Wdata::getApi($api_id);

		$request_url = $api_host.$api["url"];
		echo $request_url."<br>";

		$data = [
			'id' => $fid,
		];

		pre($data);

		$header = ["Authorization:Bearer ".$Authorization];

		$r = curl_post_header($request_url, json_encode($data), $header);

		var_dump($r);

		$r2 = json_decode($r);
		pre($r2);

	}

	/**
	 * 查看钓场认证资料
	 */
	public function state(){

		$apiid = isset($_GET["apiid"]) ? intval($_GET["apiid"]) : 0;

		$api = \libraries\waydiao\Wdata::getApi($apiid);

		$data = [
			'hosts' => $this->config->item('api_host_yuxun'),
			'api' => $api,
		];

		$this->load->view('fishingField/state', $data);

	}

	public function asyncState(){

		$api_id = isset($_POST["api_id"]) ? intval($_POST["api_id"]) : 0;
		$api_host = isset($_POST["api_host"]) ? addslashes($_POST["api_host"]) : "";
		$Authorization = $this->access_token;

		$fid = isset($_POST["fid"]) ? addslashes($_POST["fid"]) : "";

		echo $api_id."<br>";

		$api = \libraries\waydiao\Wdata::getApi($api_id);

		$request_url = $api_host.$api["url"];
		echo $request_url."<br>";

		$data = [
			'fid' => $fid,
		];

		pre($data);

		$header = ["Authorization:Bearer ".$Authorization];

		$r = curl_post_header($request_url, json_encode($data), $header);

		var_dump($r);

		$r2 = json_decode($r);
		pre($r2);

	}
}
